﻿using System;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Facade.ParvisService;
using System.Collections.Generic;
using StockholmsStad.Windows.UI.Facade.BookingService;

namespace StockholmsStad.Windows.UI.Facade
{
    public class BookingFacade : FacadeBase
    {
        /// <summary>
        /// Konstruktor. 
        /// </summary>
        /// <param name="user">Den inloggade användaren (<see cref="User"/>). </param>
        public BookingFacade(User user) : base(user)
        {
            
        }

        #region public methods

        /// <summary>
        /// Hämtar alla lediga tidsblock
        /// </summary>
        /// <returns>En lista med lediga tidsblock (<see cref="TimeBlock"/>).</returns>
        public List<TimeBlock> GetFreeTimeBlocks()
        {
            var client = new WinServiceClient();

            try
            {
                var times = new List<TimeBlock>();
                var list = client.GetFreeTimeBlocks(CurrentUser);
                times.AddRange(list);
                client.Close();
                return times;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Hämtar alla lediga tidsblock för ett visst datumintervall
        /// </summary>
        /// <returns>En lista med lediga tidsblock (<see cref="TimeBlock"/>).</returns>
        public List<TimeBlock> GetFreeTimeBlockDateTimes(DateTime fromDate, DateTime toDate)
        {
            var client = new BookingServiceClient();

            try
            {
                var times = new List<TimeBlock>();
                var list = client.GetFreeTimeBlockDateTimes(CurrentUser, fromDate, toDate);
                times.AddRange(list);
                client.Close();
                return times;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public Booking GetBooking(int ticketId)
        {
            var client = new WinServiceClient();

            try
            {
                var booking = client.GetBooking(CurrentUser, ticketId);
                client.Close();
                return booking;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void SaveBooking(int ticketId, Booking booking)
        {
            var client = new WinServiceClient();

            try
            {
                client.SaveBooking(CurrentUser, ticketId, booking);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }            
        }

        public void CancelBooking(int ticketId)
        {
            var client = new WinServiceClient();

            try
            {
                client.CancelBooking(CurrentUser, ticketId);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        #endregion 

        #region private methods

        //private static Ceremony CreateCeremonyInformationFromModel(CoupleDetailsPresentationModel coupleModel)
        //{
        //    return new Ceremony
        //                       {
        //                           CeremonyLanguage = coupleModel.CeremonyLanguage,
        //                           CertificateLanguage = coupleModel.CertificateLanguage,
        //                           Interpreter = coupleModel.Interpreter,
        //                           SpecialNeeds = new[] {coupleModel.SpecialNeed}
        //                       };
        //}

        //private static Couple CreateCoupleFromModel(CoupleDetailsPresentationModel coupleModel)
        //{
        //    var couple = new Couple
        //                     {
        //                         Person1 = new Person
        //                                       {
        //                                           FirstNames = coupleModel.Person1FirstNames,
        //                                           LastName = coupleModel.Person1LastName,
        //                                           ProtectedIdentity = coupleModel.Person1ProtectedIdentity,
        //                                           SwedishNationality =
        //                                               (coupleModel.Person1SwedishNationality.Equals("Ja")
        //                                                    ? true
        //                                                    : false),
        //                                       },

        //                         Person2 = new Person
        //                                       {
        //                                           FirstNames = coupleModel.Person2FirstNames,
        //                                           LastName = coupleModel.Person2LastName,
        //                                           SwedishNationality =
        //                                               coupleModel.Person2SwedishNationality.Equals("Ja") ? true : false
        //                                       },
        //                     };

        //    if (coupleModel.Person1Sex != null)
        //        couple.Person1.Sex = coupleModel.Person1Sex.Equals("Man") ? Sex.Male : Sex.Female;

        //    if (coupleModel.Person2Sex != null)
        //        couple.Person2.Sex = coupleModel.Person2Sex.Equals("Man") ? Sex.Male : Sex.Female;

        //    if (coupleModel.Person1PersonalNumber != null)
        //        couple.Person1.PersonalNumber = coupleModel.Person1PersonalNumber.Replace("-", "");

        //    if (coupleModel.Person2PersonalNumber != null)
        //        couple.Person2.PersonalNumber = coupleModel.Person2PersonalNumber.Replace("-", "");

        //    if (coupleModel.Person1Nationality != null)
        //        couple.Person1.Nationality = coupleModel.Person1Nationality;

        //    if (coupleModel.Person2Nationality != null)
        //        couple.Person2.Nationality = coupleModel.Person2Nationality;

        //    if (coupleModel.Person1ProtectedIdentity)
        //        couple.Person1.ProtectedIdentity = coupleModel.Person1ProtectedIdentity;

        //    if (coupleModel.Person2ProtectedIdentity)
        //        couple.Person2.ProtectedIdentity = coupleModel.Person2ProtectedIdentity;

        //    if (!(string.IsNullOrEmpty(coupleModel.StreetAddress) || string.IsNullOrEmpty(coupleModel.CoAddress)
        //          || string.IsNullOrEmpty(coupleModel.City) || string.IsNullOrEmpty(coupleModel.Country)
        //          || string.IsNullOrEmpty(coupleModel.ZipCode)))
        //    {
        //        couple.Person1.Address = new Address
        //                                     {
        //                                         StreetAddress = coupleModel.StreetAddress,
        //                                         CoAddress = coupleModel.CoAddress,
        //                                         City = coupleModel.City,
        //                                         Country = coupleModel.Country,
        //                                         PostCode = coupleModel.ZipCode
        //                                     };
        //    }

        //    if (coupleModel.ContactPersonIdentity != null)
        //    {
        //        if (coupleModel.ContactPersonIdentity.Contains(PERSON1))
        //        {
        //            // Person1 är kontaktperson
        //            couple.Person1.Email = coupleModel.Email;
        //            couple.Person1.Phonenumber = coupleModel.Phone;
        //        }
        //        else
        //        {
        //            couple.ContactPerson = new ContactPerson
        //                                       {
        //                                           Email = coupleModel.Email,
        //                                           Telephone = coupleModel.Phone,
        //                                           FirstNames = coupleModel.ContactPersonFirstName,
        //                                           LastName = coupleModel.ContactPersonLastName
        //                                       };
        //        }
        //    }
         
        //    return couple;
        //}

        //private ObservableCollection<CoupleDetailsPresentationModel> ProcessTickets(IEnumerable<Ticket> tickets)
        //{
        //    if (tickets == null)
        //        return null;

        //    var presentationModels = new ObservableCollection<CoupleDetailsPresentationModel>();

        //    // TODO temporär innan ceremoni tillagd
        //    int i = 0;
        //    foreach (var ticket in tickets)
        //    {
        //        var presentationModel = new CoupleDetailsPresentationModel
        //                                    {
        //                                        TicketId = ticket.TicketId,
        //                                        Person1FirstNames = ticket.Couple.Person1.FirstNames,
        //                                        Person1LastName = ticket.Couple.Person1.LastName,
        //                                        Person1PersonalNumber =
        //                                            string.IsNullOrEmpty(ticket.Couple.Person1.PersonalNumber)
        //                                                ? string.Empty
        //                                                :
        //                                                    (ticket.Couple.Person1.PersonalNumber.Length == 12
        //                                                         ? ticket.Couple.Person1.PersonalNumber.Insert(8,
        //                                                                                                       "-")
        //                                                         : ticket.Couple.Person1.PersonalNumber),
        //                                        Person1ProtectedIdentity = ticket.Couple.Person1.ProtectedIdentity,
        //                                        Person1SwedishNationality =
        //                                            (ticket.Couple.Person1.SwedishNationality ? "Ja" : "Nej"),
        //                                        Person1Nationality = ticket.Couple.Person1.Nationality,

        //                                        StreetAddress =
        //                                            ticket.Couple.Person1.Address != null
        //                                                ? ticket.Couple.Person1.Address.StreetAddress
        //                                                : string.Empty,
        //                                        CoAddress =
        //                                            ticket.Couple.Person1.Address != null
        //                                                ? ticket.Couple.Person1.Address.CoAddress
        //                                                : string.Empty,
        //                                        ZipCode =
        //                                            ticket.Couple.Person1.Address != null
        //                                                ? ticket.Couple.Person1.Address.PostCode
        //                                                : string.Empty,
        //                                        City =
        //                                            ticket.Couple.Person1.Address != null
        //                                                ? ticket.Couple.Person1.Address.City
        //                                                : string.Empty,
        //                                        Country =
        //                                            ticket.Couple.Person1.Address != null
        //                                                ? ticket.Couple.Person1.Address.Country
        //                                                : string.Empty,

        //                                        Person2FirstNames = ticket.Couple.Person2.FirstNames,
        //                                        Person2LastName = ticket.Couple.Person2.LastName,
        //                                        Person2PersonalNumber =
        //                                            string.IsNullOrEmpty(ticket.Couple.Person2.PersonalNumber)
        //                                                ? string.Empty
        //                                                :
        //                                                    (ticket.Couple.Person2.PersonalNumber.Length == 12
        //                                                         ? ticket.Couple.Person2.PersonalNumber.Insert(8,
        //                                                                                                       "-")
        //                                                         : ticket.Couple.Person2.PersonalNumber),
        //                                        Person2ProtectedIdentity = ticket.Couple.Person2.ProtectedIdentity,
        //                                        Person2SwedishNationality =
        //                                            (ticket.Couple.Person2.SwedishNationality ? "Ja" : "Nej"),
        //                                        Person2Nationality = ticket.Couple.Person2.Nationality

        //                                    };

        //        if (ticket.Couple.Person1.Sex != null)
        //        {
        //            presentationModel.Person1Sex = Enum.GetName(typeof (Sex), ticket.Couple.Person1.Sex).Equals("Male")
        //                                               ? "Man"
        //                                               : "Kvinna";
        //        }

        //        if (ticket.Couple.Person2.Sex != null)
        //        {
        //            presentationModel.Person2Sex = Enum.GetName(typeof (Sex), ticket.Couple.Person2.Sex).Equals("Male")
        //                                               ? "Man"
        //                                               : "Kvinna";
        //        }


        //        presentationModel.SP = ticket.Couple.Person1.ProtectedIdentity ||
        //                               ticket.Couple.Person2.ProtectedIdentity
        //                                   ? "Ja"
        //                                   : "";

        //        if (ticket.Couple.ContactPerson == null)
        //        {
        //            // Person1 är kontaktperson
        //            presentationModel.ContactPersonIdentity = GetContactTypes()[0];
        //            presentationModel.ContactPersonFirstName = ticket.Couple.Person1.FirstNames;
        //            presentationModel.ContactPersonLastName = ticket.Couple.Person1.LastName;
        //            presentationModel.Email = string.IsNullOrEmpty(ticket.Couple.Person1.Email)
        //                                          ? string.Empty
        //                                          : ticket.Couple.Person1.Email;
        //            presentationModel.Phone = string.IsNullOrEmpty(ticket.Couple.Person1.Phonenumber)
        //                                          ? string.Empty
        //                                          : ticket.Couple.Person1.Phonenumber;
        //        }
        //        else
        //        {
        //            // Annan person är kontaktperson
        //            presentationModel.ContactPersonIdentity = GetContactTypes()[1];
        //            presentationModel.ContactPersonFirstName = ticket.Couple.ContactPerson.FirstNames;
        //            presentationModel.ContactPersonLastName = ticket.Couple.ContactPerson.LastName;
        //            presentationModel.Email = ticket.Couple.ContactPerson.Email;
        //            presentationModel.Phone = ticket.Couple.ContactPerson.Telephone;
        //        }

        //        // ceremony info
        //        if (i%5 == 0)
        //        {
        //            presentationModel.Date = DateTime.Now.ToString("yyyy-MM-dd");
        //            presentationModel.Status = "Påbörjad";
        //            presentationModel.HP = "";
        //            presentationModel.SP = "Ja";
        //            presentationModel.Note = "Mamma spelar gitarr och pappa sjunger";
        //            presentationModel.CeremonyType = "Stadshuset";
        //        }
        //        else if (i%3 == 0)
        //        {
        //            presentationModel.Date = DateTime.Now.ToString("yyyy-MM-dd");
        //            presentationModel.Status = "Bekräftad";
        //            presentationModel.HP = "Inkom 2008-09-10";
        //            presentationModel.SP = "";
        //            presentationModel.CeremonyType = "Stadshuset";
        //        }
        //        else
        //        {
        //            presentationModel.Date = DateTime.Now.ToString("yyyy-MM-dd");
        //            presentationModel.Status = "Avslutad";
        //            presentationModel.HP = "Inkom 2008-05-10";
        //            presentationModel.SP = "";
        //            presentationModel.CeremonyType = "Individuell";
        //        }

        //        presentationModels.Add(presentationModel);

        //        #region ceremonyItems

        //        presentationModel.CeremonyType = "Individuell";
        //        presentationModel.MinisterFirstName = "Per";
        //        presentationModel.MinisterLastName = "Persson";
        //        presentationModel.HPArrivalDate = DateTime.Now.ToString("yyyy-MM-dd");
        //        presentationModel.HPHasArrived = true;
        //        presentationModel.HPApplicationDate = DateTime.Now.ToString("yyyy-MM-dd");
        //        presentationModel.SpecialNeed = "Rullstol";
        //        presentationModel.Interpreter = true;

        //        #endregion

        //        i++;
        //    }
        //    return presentationModels;
        //}

        //private ObservableCollection<CoupleDetailsPresentationModel> PopulateCouples()
        //{
        //    var couples = LoadCouples();
        //    foreach (CoupleDetailsPresentationModel couple in couples)
        //    {
        //        if (!string.IsNullOrEmpty(couple.ContactPersonFirstName) && !string.IsNullOrEmpty(couple.ContactPersonLastName))
        //        {
        //            couple.ContactPersonIdentity = GetContactTypes()[1];
        //        }
        //        else
        //        {
        //            couple.ContactPersonIdentity = GetContactTypes()[0];
        //            couple.ContactPersonFirstName = couple.Person1FirstNames;
        //            couple.ContactPersonLastName = couple.Person1LastName;
        //        }
        //    }

        //    return couples;
        //}

        // temp metod för lokal testning utan wcf
        //private static ObservableCollection<CoupleDetailsPresentationModel> LoadCouples()
        //{
        //    var couples = new ObservableCollection<CoupleDetailsPresentationModel>
        //                  {
        //                      new CoupleDetailsPresentationModel
        //                    {
        //                        TicketId = 1,
        //                        Person1FirstNames = "Kalle",
        //                        Person1LastName = "andersson",
        //                        Person1PersonalNumber = "19721212-3445",
        //                        Person1Sex = "Man",
        //                        Person1ProtectedIdentity = false,
        //                        Person1SwedishNationality = "Ja",
        //                        Email = "kalle@abc.com",
        //                        Date = DateTime.Now.ToString("yyyy-MM-dd"),
        //                        Person2FirstNames = "erika",
        //                        Person2LastName = "minnasson",
        //                        Person2PersonalNumber = "19790312-9876",
        //                        Person2Sex = "Kvinna",
        //                        StreetAddress = "Karlavägen 4",
        //                        ZipCode = "12223",
        //                        Country = "Sverige",
        //                        City = "stockholm",
        //                        Phone = "08-123455",
        //                        Person2SwedishNationality = "Nej",
        //                        Person2Nationality = "Norsk",
        //                        Person2ProtectedIdentity = false,
        //                        Status = "påbörjad",
        //                        ContactPersonFirstName = "KontaktNamn",
        //                        ContactPersonLastName = "KontaktEfternamn",
        //                    },
        //                    new CoupleDetailsPresentationModel
        //            {
        //                TicketId = 2,
        //                Person1FirstNames = "linda (hemlig)",
        //                Person1LastName = "karlsson",
        //                Person1PersonalNumber = "",
        //                Person1ProtectedIdentity = true,
        //                Person1Sex = "Kvinna",
        //                Person1SwedishNationality = "Ja",
        //                Email = "linda@abc.com",
        //                StreetAddress = "Sveavägen 4",
        //                ZipCode = "12327",
        //                Phone = "08-6734657",
        //                City = "stockholm",
        //                Country = "Sverige",
        //                Date = DateTime.Now.ToString("yyyy-MM-dd"),
        //                Person2FirstNames = "martin",
        //                Person2LastName = "eriksson",
        //                Person2Sex = "Man",
        //                Person2SwedishNationality = "Nej",
        //                Person2Nationality = "Finland",
        //                Person2PersonalNumber = "19790312-9876",
        //                Person2ProtectedIdentity = false,
        //                Status = "avslutad"
        //            },
        //        new CoupleDetailsPresentationModel
        //            {
        //                TicketId = 3,
        //                Person1FirstNames = "mats",
        //                Person1LastName = "magnusson",
        //                Person1PersonalNumber = "19800101-1212",
        //                Person1Sex = "Man",
        //                Person1ProtectedIdentity = false,
        //                Person2ProtectedIdentity = false,
        //                Date = DateTime.Now.ToString("yyyy-MM-dd"),
        //                        StreetAddress = "Kallevägen 4",
        //                        ZipCode = "11256",
        //                Person2FirstNames = "niklas",
        //                Person2LastName = "lind",
        //                Person2PersonalNumber = "19831101-1452",
        //                Person2Sex = "Man",
        //                City = "lund",
        //                Country = "Sverige",
        //                Phone = "08-2346567",
        //                Email = "mats@abc.com",
        //                Person1SwedishNationality = "Ja",
        //                Person2SwedishNationality = "Nej",
        //                Person2Nationality = "Norsk",
        //                Status = "påbörjad"
        //            },
        //        new CoupleDetailsPresentationModel
        //            {
        //                TicketId = 4,
        //                Person1FirstNames = "anna",
        //                Person1LastName = "andersson",
        //                Person1Sex = "Kvinna",
        //                Date = DateTime.Now.ToString("yyyy-MM-dd"),
        //                Person2FirstNames = "lina",
        //                Person2LastName = "lundh",
        //                Person2Sex = "Kvinna",
        //                City = "stockholm",
        //                StreetAddress = "Matsgatan 3",
        //                ZipCode = "12112",
        //                Phone = "08-232323",
        //                Country = "Norge",
        //                Person1SwedishNationality = "Ja",
        //                Person2SwedishNationality = "Nej",
        //                Person2Nationality = "Norsk",
        //                Status = "påbörjad",
        //                ContactPersonFirstName = "MittNamn",
        //                ContactPersonLastName = "MittEfetrnamn",
        //                Email = "konakt@hotmail.com",
        //            }
        //    };

        //    return couples;
        //}

        #endregion
    }
}
